package ch.fusun.baron.basic.initialization;

import ch.fusun.baron.basic.messages.Messages;
import ch.fusun.baron.core.initialization.GameInitializer;
import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.farm.FarmService;
import ch.fusun.baron.map.Tile;
import ch.fusun.baron.map.api.GameMapService;
import ch.fusun.baron.player.api.Country;
import ch.fusun.baron.player.api.CountryService;
import ch.fusun.baron.printing.MessagingService;
import ch.fusun.baron.property.api.PropertyService;

/**
 * Adds a farm for every player
 */
public class FarmInitializer implements GameInitializer {

	@Inject
	private FarmService farmService;
	@Inject
	private PropertyService propertyService;
	@Inject
	private GameMapService mapService;
	@Inject
	private MessagingService messagingService;
	@Inject
	private transient CountryService countryService;

	/**
	 * Constructor
	 * 
	 */
	public FarmInitializer() {
	}

	@Override
	public void initialize() {
		for (Country country : countryService.getCountries()) {
			initializeDynasty(country);
		}
	}

	private void initializeDynasty(Country country) {
		for (Tile tile : mapService.getTiles()) {
			if (country.equals(propertyService.getOwnership(tile))) {
				farmService.createFarm(tile);
				return;
			}
		}
		messagingService.addMessage(null,
				Messages.FarmInitializer_FarmInitialized);
	}
}
